package com.newtouch.bxzs.database.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.newtouch.bxzs.common.base.model.bo.DataDictionaryValueBO;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.DataDictionaryBO;
import com.newtouch.bxzs.database.model.vo.DataDictionaryVO;
import com.newtouch.bxzs.database.service.DataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典管理接口的controller类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/4</td>
 * <td>zhangqianyang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author zhangqianyang
 * @version 1.0.0
 * @since 1.0.0
 */

@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/dataDictionaryController")
@SuppressWarnings("unchecked")
public class DataDictionaryController {
	@Resource
	private DataDictionaryService dataDictionaryService;

	@Autowired
	private RedisUtils redisUtils;

	/**
	 * 静态变量：日志
	 */
	private static final Log logger = LogFactory.getLog(DataDictionaryController.class);

	@ApiOperation("获取数据字典列表")
	@PostMapping("/getDataDictionaryList")
	@MyLog(systemFunctionName = "数据字典管理", systemFunctionUrl = "/dataDictionaryController/getDataDictionaryList", userOperationType = 4, userOperationSummary = "获取数据字典列表")
	public BaseResponse<DataDictionaryBO> getDataDictionaryList(
			@RequestBody(required = false) @ApiParam(name = "数据字典名称") String dataDictionaryName) {
		logger.info("开始获取数据字典列表");
		return RespGenerator.returnOK(dataDictionaryService.getDataDictionaryList(dataDictionaryName));
	}

	@ApiIgnore
	@ApiOperation("新增数据字典(用于后端测试)")
	@PostMapping("/modifyDataDictionary")
	public BaseResponse<String> modifyDataDictionary(
			@RequestBody DataDictionaryVO dataDictionaryVO) {
		logger.info("新增数据字典");
		return RespGenerator.returnOK(dataDictionaryService.modifyDataDictionary(dataDictionaryVO));
	}

	@ApiOperation("根据字典code获取字典值")
	@GetMapping("/getDataDictionaryEntry")
	@MyLog(systemFunctionName = "数据字典管理", systemFunctionUrl = "/dataDictionaryController/getDataDictionaryEntry", userOperationType = 4, userOperationSummary = "根据字典code获取字典值")
	public BaseResponse<List<DataDictionaryValueBO>> getDataDictionaryEntry(
			@ApiParam(value = "字典编码code", required = true) @RequestParam String dictionaryCode) {
		logger.info("根据字典code获取字典值");
		List<DataDictionaryValueBO> dictValues = JSONArray.parseArray(redisUtils.get(dictionaryCode),
				DataDictionaryValueBO.class);
		if (CollectionUtil.isEmpty(dictValues)) {
			// 更新缓存
			dictValues = dataDictionaryService.getDataDictionaryEntry(dictionaryCode.toUpperCase());
			redisUtils.getAndSet(dictionaryCode.toUpperCase(), JSON.toJSONString(dictValues));
		}
		return RespGenerator.returnOK(dictValues);
	}

	/**
	 * 启动加载字典数据至redis
	 */
	@PostConstruct
	private void getAllDataDictionaryEntry() {
		logger.info("容器初始化，执行查询字典值...");
		HashMap<String, List<DataDictionaryValueBO>> map = dataDictionaryService.getAllDataDictionaryEntry();
		for (Map.Entry<String, List<DataDictionaryValueBO>> entry : map.entrySet()) {
			redisUtils.getAndSet(entry.getKey().toUpperCase(), JSON.toJSONString(entry.getValue()));
		}
	}
}
